/*
 * checkCommon.c
 *
 *  Created on: Mar 10, 2012
 */

/**
 * See: http://check.sourceforge.net/doc/check_html/check_3.html#SEC4
 */
#include <stdlib.h>

#include "checkConfigurationHandler.h"

#include <sys/types.h>
// defines sock_stream
#include <sys/socket.h>

// define strlen().
#include <string.h>

// for printf.
#include <stdio.h>

#include "ConfigurationHandler.h"

int PutXElementInVariableList(int nEntriesToFill) {
	int nReturnCode = -1;
	char szVariableName[MAX_LENGT_OF_VARIABLE];

	int nCount = 0;
	for (nCount; nCount < nEntriesToFill; nCount++) {
		snprintf(szVariableName, MAX_LENGT_OF_VARIABLE - 1,
				"NOT_THE_VAR_YOU_ARE_LOOKING_FOR_%d", nCount);
		nReturnCode = ConfigurationVariableSetBooleanTrue(szVariableName);
	}
	return nReturnCode;
}

START_TEST( test_ConfigurationVariableClearHash)
	{
		int nReturnCode = ConfigurationVariableClearHash();
		// TODO V Extend this to actually verify the list that it has been cleared.
		fail_unless(nReturnCode == 0, "Failed ConfigurationVariableClearHash().");
	}END_TEST

START_TEST( test_ConfigurationVariableExist)
	{
		int nReturnCode = ConfigurationVariableClearHash();
		if (nReturnCode == 0) {
			nReturnCode = ConfigurationVariableSetBooleanTrue("EXISTS");
		}
		if (nReturnCode == 0) {
			nReturnCode = ConfigurationVariableExist("EXISTS");
		}
		// TODO V Extend this to actually verify the list that it has been cleared.
		fail_unless(nReturnCode == 1, "Failed ConfigurationVariableExist().");
	}END_TEST

START_TEST( test_ConfigurationVariableExist_EmptyList)
	{
		int nReturnCode = ConfigurationVariableClearHash();
		if (nReturnCode == 0) {
			nReturnCode = ConfigurationVariableExist("EXISTS");
		}
		// TODO V Extend this to actually verify the list that it has been cleared.
		fail_unless(nReturnCode == 0, "Failed ConfigurationVariableExist().");
	}END_TEST
START_TEST( test_ConfigurationVariableExist_Coverage)
	{
		// This test exists to get full coverage of the function.
		int nReturnCode = ConfigurationVariableExist("");
		fail_unless(nReturnCode == 0, "Failed ConfigurationVariableExist().");
	}END_TEST

START_TEST( test_ConfigurationVariableExist_NotThere)
	{
		int nReturnCode = ConfigurationVariableClearHash();
		if (nReturnCode == 0) {
			nReturnCode = ConfigurationVariableSetBooleanTrue("EXISTS");
		}
		if (nReturnCode == 0) {
			nReturnCode = ConfigurationVariableExist("DOES_NOT_EXIST");
		}
		// TODO V Extend this to actually verify the list that it has been cleared.
		fail_unless(nReturnCode == 0, "Failed ConfigurationVariableExist().");
	}END_TEST
START_TEST( test_ConfigurationVariableLookUpName)
	{
		int nReturnCode = ConfigurationVariableLookUpName("DOES_NOT_EXIST");
		fail_unless(nReturnCode == -1, "Failed ConfigurationVariableLookUpName().");
	}END_TEST

START_TEST( test_ConfigurationVariableLookUpName_EntryExists_FirstEntry)
	{
		int nReturnCode = ConfigurationVariableClearHash();
		if (nReturnCode == 0) {
			nReturnCode = ConfigurationVariableSetBooleanTrue("EXISTS");
		}
		if (nReturnCode == 0) {
			nReturnCode = ConfigurationVariableLookUpName("EXISTS");
		}
		fail_unless(nReturnCode == 0, "Failed ConfigurationVariableLookUpName().");
	}END_TEST

START_TEST( test_ConfigurationVariableLookUpName_EntryExists_NoDuplicateEntry)
	{
		int nReturnCode = ConfigurationVariableClearHash();
		if (nReturnCode == 0) {
			int nCount = 0;
			for (nCount; nCount < MAX_NUMBER_OF_VARIABLES - 1; nCount++) {
				nReturnCode = ConfigurationVariableSetBooleanTrue(
						"NOT_THE_VAR_YOU_ARE_LOOKING_FOR");
			}
			nReturnCode = ConfigurationVariableSetBooleanTrue("EXISTS");
		}
		if (nReturnCode == 0) {
			nReturnCode = ConfigurationVariableLookUpName("EXISTS");
		}
		fail_unless(nReturnCode == 1, "Failed ConfigurationVariableLookUpName().");
	}END_TEST

START_TEST( test_ConfigurationVariableLookUpName_EntryExists_LastEntry)
	{

		int nReturnCode = ConfigurationVariableClearHash();
		if (nReturnCode == 0) {
			nReturnCode
					= PutXElementInVariableList(MAX_NUMBER_OF_VARIABLES - 1);
			nReturnCode = ConfigurationVariableSetBooleanTrue("EXISTS");
		}
		if (nReturnCode == 0) {
			nReturnCode = ConfigurationVariableLookUpName("EXISTS");
		}
		printf("III %d\n", nReturnCode);
		fail_unless(nReturnCode == MAX_NUMBER_OF_VARIABLES-1, "Failed ConfigurationVariableLookUpName().");
	}END_TEST

START_TEST( test_ConfigurationVariableGetUnusedEntry_FullList)
	{
		int nReturnCode = ConfigurationVariableClearHash();
		nReturnCode = PutXElementInVariableList(MAX_NUMBER_OF_VARIABLES);
		nReturnCode = ConfigurationVariableGetUnusedEntry();
		fail_unless(nReturnCode == -1, "Failed ConfigurationVariableGetUnusedEntry() full list.");
	}END_TEST

START_TEST( test_ConfigurationVariableGetUnusedEntry)
	{
		ConfigurationVariableClearHash();
		int nReturnCode = ConfigurationVariableGetUnusedEntry();
		fail_unless(nReturnCode == 0, "Failed ConfigurationVariableGetUnusedEntry().");
	}END_TEST

START_TEST( test_ConfigurationVariableSetBooleanTrue)
	{
		ConfigurationVariableClearHash();
		int nReturnCode = ConfigurationVariableSetBooleanTrue("Nice_Name");
		fail_unless(nReturnCode == 0, "Failed ConfigurationVariableSetBooleanTrue().");
	}END_TEST

START_TEST( test_ConfigurationVariableSetBooleanTrue_FullList)
	{
		int nReturnCode = ConfigurationVariableClearHash();
		nReturnCode = PutXElementInVariableList(MAX_NUMBER_OF_VARIABLES);
		nReturnCode = ConfigurationVariableSetBooleanTrue("Nice_Name");
		;
		fail_unless(nReturnCode == -3, "Failed ConfigurationVariableSetBooleanTrue() full list.");
	}END_TEST

START_TEST( test_ConfigurationHandlerInitialization)
	{
		int nEntries = 4;
		char *szVersionEntry[4];
		char szCommand[MAX_LENGT_OF_VARIABLE] = "checkCommon.c";
		char szVersionText[MAX_LENGT_OF_VARIABLE] = "--version";
		char szHelpText[MAX_LENGT_OF_VARIABLE] = "--help";
		char szTcv[MAX_LENGT_OF_VARIABLE] = "--tcv";
		szVersionEntry[0] = szCommand;
		szVersionEntry[1] = szHelpText;
		szVersionEntry[2] = szVersionText;
		szVersionEntry[3] = szTcv;
		//snprintf(szVersionEntry[0], MAX_LENGT_OF_VARIABLE, "checkCommon.c");
		//snprintf(szVersionEntry[1], MAX_LENGT_OF_VARIABLE, "--version");


		int nReturnCode = ConfigurationHandlerInitialization(nEntries,
				szVersionEntry);
		fail_unless(nReturnCode == 0, "Failed ConfigurationHandlerInitialization(4,\"checkCommon.c\",\"--version\",...).");
	}END_TEST

START_TEST( test_ConfigurationHandlerInitialization_UnknownEntry)
	{
		int nEntries = 2;
		char *szVersionEntry[2];
		char szCommand[MAX_LENGT_OF_VARIABLE] = "checkCommon.c";
		char szUnknownText[MAX_LENGT_OF_VARIABLE] = "--unknown";
		szVersionEntry[0] = szCommand;
		szVersionEntry[1] = szUnknownText;
		//snprintf(szVersionEntry[0], MAX_LENGT_OF_VARIABLE, "checkCommon.c");
		//snprintf(szVersionEntry[1], MAX_LENGT_OF_VARIABLE, "--version");


		int nReturnCode = ConfigurationHandlerInitialization(nEntries,
				szVersionEntry);
		fail_unless(nReturnCode == -1, "Failed ConfigurationHandlerInitialization(0,NULL).");
	}END_TEST
START_TEST( test_ConfigurationHandlerInitialization_ValidateExists)
	{
		int nEntries = 4;
		char *szVersionEntry[4];
		char szCommand[MAX_LENGT_OF_VARIABLE] = "checkCommon.c";
		char szVersionText[MAX_LENGT_OF_VARIABLE] = "--version";
		char szHelpText[MAX_LENGT_OF_VARIABLE] = "--help";
		char szTcv[MAX_LENGT_OF_VARIABLE] = "--tcv";
		szVersionEntry[0] = szCommand;
		szVersionEntry[1] = szVersionText;
		szVersionEntry[2] = szHelpText;
		szVersionEntry[3] = szTcv;

		int nReturnCode = ConfigurationHandlerInitialization(nEntries,
				szVersionEntry);
		if (nReturnCode == 0) {
			nReturnCode = ConfigurationVariableExist(VERSION_VARIABLE_NAME);
		} else {
			nReturnCode = -1;
		}

		fail_unless(nReturnCode == 1, "Failed ConfigurationHandlerInitialization() vars can be read.");
	}END_TEST

START_TEST( test_ConfigurationHandlerInitialization_EmptyData)
	{
		int nReturnCode = ConfigurationHandlerInitialization(0, NULL);
		fail_unless(nReturnCode == 0, "Failed ConfigurationHandlerInitialization(0,NULL).");
	}END_TEST

START_TEST( test_IsValidVariableName )
	{
		int nReturnCode = IsValidVariableName("VALID");
		fail_unless(nReturnCode == 0, "Failed IsValidVariableName(\"VALID\").");
	}END_TEST

START_TEST( test_IsValidVariableName_null )
	{
		int nReturnCode = IsValidVariableName(NULL);
		fail_unless(nReturnCode == -1, "Failed IsValidVariableName(NULL).");
	}END_TEST

START_TEST( test_IsValidVariableName_too_long )
	{
		char szTooLong[MAX_LENGT_OF_VARIABLE + 2];
		int nCount = 0;
		for (nCount = 0; nCount < MAX_LENGT_OF_VARIABLE + 2; nCount++) {
			szTooLong[nCount] = 'X';
		}
		szTooLong[MAX_LENGT_OF_VARIABLE + 1] = '\0';

		int nReturnCode = IsValidVariableName(szTooLong);
		fail_unless(nReturnCode == -3, "Failed IsValidVariableName(szTooLong).");
	}END_TEST

START_TEST( test_IsValidVariableName_EmptyString )
	{
		int nReturnCode = IsValidVariableName("");
		fail_unless(nReturnCode == -2, "Failed IsValidVariableName(\"\").");
	}END_TEST

Suite *configuration_handler_suite(void) {
	Suite *s = suite_create("ConfigurationHandler");

	/* Core test case */
	TCase *tc_core = tcase_create("Core");
	tcase_add_test(tc_core, test_IsValidVariableName);
	tcase_add_test(tc_core, test_ConfigurationVariableClearHash);
	tcase_add_test(tc_core, test_ConfigurationVariableLookUpName);
	tcase_add_test(tc_core, test_ConfigurationVariableGetUnusedEntry);
	tcase_add_test(tc_core, test_ConfigurationVariableSetBooleanTrue);
	tcase_add_test(tc_core, test_ConfigurationVariableExist);
	tcase_add_test(tc_core, test_ConfigurationVariableExist_EmptyList);
	tcase_add_test(tc_core, test_ConfigurationVariableExist_NotThere);
	tcase_add_test(tc_core, test_ConfigurationHandlerInitialization);
	tcase_add_test(tc_core, test_ConfigurationHandlerInitialization_UnknownEntry);
	tcase_add_test(tc_core, test_ConfigurationHandlerInitialization_ValidateExists);
	suite_add_tcase(s, tc_core);

	// TODO V Also Run the tests where some vars have been deleted.

	TCase *tc_limits = tcase_create("Limits");
	tcase_add_test (tc_limits, test_IsValidVariableName_null);
	tcase_add_test (tc_limits, test_IsValidVariableName_too_long);
	tcase_add_test (tc_limits, test_IsValidVariableName_EmptyString);
	tcase_add_test (tc_limits, test_ConfigurationVariableLookUpName_EntryExists_FirstEntry);
	tcase_add_test (tc_limits, test_ConfigurationVariableLookUpName_EntryExists_LastEntry);
	tcase_add_test (tc_limits, test_ConfigurationVariableLookUpName_EntryExists_NoDuplicateEntry);
	tcase_add_test (tc_limits, test_ConfigurationVariableGetUnusedEntry_FullList);
	tcase_add_test (tc_limits, test_ConfigurationVariableSetBooleanTrue_FullList);
	tcase_add_test (tc_limits, test_ConfigurationHandlerInitialization_EmptyData);
	tcase_add_test (tc_limits, test_ConfigurationVariableExist_Coverage);
	suite_add_tcase(s, tc_limits);

	return s;
}

